<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Http\Controllers\BaseController;
use App\Http\Validate\OpenShop\ShopEngineeringConstruction\ShopEngineeringConstructionValidate;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Service\OpenShop\Feishu\SelectionService;
use App\Service\OpenShop\Feishu\ShopEngineeringConstructionService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ShopEngineeringConstructionController extends BaseController
{

    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws Exception
     */
    public function engineeringConstructionStart(Request $request, string $orderNo): JsonResponse
    {
        $requestParams = $request->merge(["orderNo" => $orderNo])->all();
        $actuallyParams = ShopEngineeringConstructionValidate::engineeringConstructionStartValidate($requestParams);
        ShopEngineeringConstructionService::engineeringConstructionStart(getRmsUserInfoByAuth(), $actuallyParams);
        return $this->success();
    }

    /**
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function engineeringConstructionFinish(Request $request, string $orderNo): JsonResponse
    {
        $requestParams = $request->merge(["orderNo" => $orderNo])->all();
        $finishData = ShopEngineeringConstructionValidate::engineeringConstructionFinish($requestParams);
        $attachData = ShopEngineeringConstructionValidate::attachValidate($requestParams);
        ShopEngineeringConstructionService::engineeringConstructionFinish(getRmsUserInfoByAuth(), $finishData, $attachData);
        return $this->success();
    }

    /**
     * @param string $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function startDetail(string $orderNo): JsonResponse
    {
        ShopEngineeringConstructionValidate::startDetailValidate(["orderNo" => $orderNo]);
        return $this->success(ShopEngineeringConstructionService::detail($orderNo));
    }

    /**
     * @param string $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function finishDetail(string $orderNo): JsonResponse
    {
        ShopEngineeringConstructionValidate::finishDetailValidate(["orderNo" => $orderNo]);
        return $this->success(ShopEngineeringConstructionService::detail($orderNo));
    }


    /**
     * 营建更新数据
     *
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     * @throws Exception
     */
    public function update(Request $request, string $orderNo): JsonResponse
    {
        $requestParams = $request->merge(["orderNo" => $orderNo])->all();
        $finishData = ShopEngineeringConstructionValidate::engineeringConstructionUpdate($requestParams);
        $attachData = ShopEngineeringConstructionValidate::attachValidate($requestParams);
        ShopEngineeringConstructionService::update(getRmsUserInfoByAuth(), $finishData, $attachData);
        return $this->success();
    }
}
